<?php
class admin{
	
	function loginCheck($email, $password) {
		global $con;
		$sql = "SELECT user_email, user_password, user_id ,user_fname FROM tbl_admin WHERE 
		user_email='".mysqli_escape_string($con,$email)."' AND 
		user_password='".mysqli_escape_string($con,md5($password))."'";
		
		$res = mysqli_query($con, $sql);
		
		if (mysqli_num_rows($res) > 0) {
			
			$rec = mysqli_fetch_array($res);
			$_SESSION['userId']=$rec['user_id'];
			$_SESSION['userfname']=$rec['user_fname'];
			header("location:dashboard.php");
		} else {
			return "Login Fail!!!";
		}
	}
	
	
	//adminuser selection function
	function selectAdmin()
	{
		global $con;
		$sql="select * from tbl_admin";
		$res = mysqli_query($con,$sql);
		return $res;
	}/// EOF
	
	
	//function for adding admin user
	
	function insertAdmin($fname,$lname,$email,$password)
	{
		global $con;
		$sql=sprintf("INSERT INTO tbl_admin SET 
							user_fname='%s',
							user_lname='%s',
							user_email='%s',
							user_password='%s'",
							mysqli_escape_string($con,$fname),
							mysqli_escape_string($con,$lname),
							mysqli_escape_string($con,$email),
							mysqli_escape_string($con,$password));
							
		$result= mysqli_query($con,$sql);
		return $result;
		
		
	}/// EOF
	
	//function for updating faq
	function updateAdmin($id,$fname,$lname,$email,$password){
		
		global $con;
		echo $sql=sprintf("update tbl_admin SET 
							user_fname='%s',
							user_lname='%s',
							user_email='%s',
							user_password='%s' where user_id='%d'",
							mysqli_escape_string($con,$fname),
							mysqli_escape_string($con,$lname),
							mysqli_escape_string($con,$email),
							mysqli_escape_string($con,$password),
							mysqli_escape_string($con,$id));
		$res = mysqli_query($con,$sql);
		if(!$res){
			die("Sql error " . mysqli_error($con));
		}
		return $res;	
	
	}//EOF
	
	//function for deleting faq
	function deleteAdmin($id)
	{
		global $con;
		$sql="DELETE FROM tbl_admin WHERE user_id='$id' ";
		mysqli_query($con,$sql);
	}
		
	//function selecting admin by id
	function getAdminById($adminId){
		global $con;
		$sql= "select * from tbl_admin where user_id='$adminId'";
		$res= mysqli_query($con,$sql);
		return $res;
	}
	
}	


$objAdmin =new admin();
?>
